var utils = (function () {
    //初始化 页面 载入头部和菜单 完成后执行 callback
    var initOutFrame = function (option, callback) {
        var optionFinal = $.extend(true, {}, {
            sidebar_target: ''
        }, option);
        var inner = $(".main-container .inner-container");
        inner.remove();
        $(".main-container").load("/ums/pg/outframeinfo.html .outframe", function (res) {
            $(".page-sidebar").removeClass("active");
            $(".page-sidebar[pagename='" + optionFinal.sidebar_target + "']").addClass("active");
            $(".content-main").append(
                inner
            )
            $("#ums_real_name").text(localStorage.ums_real_name)
            inner.show();
            callback($(".content-main"))
        });
    };

    var showBtnEnable = function (count) {
        if (count > 0) {
            $(".btn_forany")
                .removeAttr("disabled")
                .attr("data-toggle", "modal")
        } else {
            $(".btn_forany")
                .attr("disabled", "disabled")
                .removeAttr("data-toggle")
        }
        if (count == 1) {
            $(".btn_forsingle")
                .removeAttr("disabled")
                .attr("data-toggle", "modal")
        } else {
            $(".btn_forsingle")
                .attr("disabled", "disabled")
                .removeAttr("data-toggle")
        }
    };

    var sendPost = function (url, data, success, fail) {
        $.ajax({
            type: 'POST',
            url: url,
            contentType: 'application/json',
            processData: false,
            data: JSON.stringify(data),
            dataType: 'json',
            success: function (res) {
                success(res);
            },
            error: function () {
                tableHelper.showMsg('错误', 0);
                if (fail) {
                    fail();
                }
            }
        });
    };
    var gotoLoginPage = function () {
        delete localStorage.ums_access_token;
        window.location.href = "/ums/pg/login.html";
    };

    /**
     * 只是判断一下是否有token 没有就进入登录页
     */
    var checkToken = function () {
        if (!localStorage.ums_access_token) {
            gotoLoginPage();
        }
    };

    var logout = function () {
        gotoLoginPage();
    };

    /**
     * 判断表单是否缺少字段
     * @param target_out_select
     * @returns {boolean}
     * @private
     */
    var _isNotFillTotally = function (target_out_select) {
        _clearBitian(target_out_select);
        console.log('start->');
        var errcount = 0;
        $(target_out_select + " .bitian").each(function () {
            var value = $(this).val();
            if (!value) {
                ++errcount;
                console.log('match->', $(this));
                $(this).parent().addClass("has-error");
            }
        });
        if (errcount > 0) {
            tableHelper.showMsg("错误或者不完整的表单", 0);
        }
        return errcount > 0;
    }

    /**
     * 清除必填的错误
     * @param target_out_select
     * @returns {boolean}
     * @private
     */
    var _clearBitian = function (target_out_select) {
        $(target_out_select + " .bitian").parent().removeClass("has-error")
        return false;
    }
    return {
        initOutFrame: initOutFrame,
        showBtnEnable: showBtnEnable,
        sendPost: sendPost,
        gotoLoginPage: gotoLoginPage,
        checkToken: checkToken,
        logout: logout,
        _isNotFillTotally: _isNotFillTotally,
        _clearBitian: _clearBitian,
    }
})();